package swimv2gm.client.requests;

import java.io.IOException;
import java.util.List;

import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import swimv2gm.bl.session.WorkRequestManager;
import swimv2gm.bl.util.WorkRequestData;

/**
 * Carica la lista delle richieste di lavoro inviate o ricevute.
 */
public class MyRequests extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException {
		WorkRequestManager man;
		try {
			man = (WorkRequestManager)InitialContext.doLookup("WorkRequestManager");
		} catch (NamingException e) {
			response.sendError(500);
			return;
		}

		Long uid = (Long)request.getSession().getAttribute("user");

		// forbid access if the user is not connected or is an administrator
		if (uid == null
				|| (request.getSession().getAttribute("admin") != null && (Boolean)request.getSession().getAttribute(
						"admin"))) {
			response.sendError(403);
			return;
		}

		List<WorkRequestData> received = man.getReceivedWorkRequestsData(uid);
		request.setAttribute("received",received);

		List<WorkRequestData> sent = man.getSentWorkRequestsData(uid);
		request.setAttribute("sent",sent);

		request.getRequestDispatcher("myRequests.jsp").forward(request,response);
	}

	protected void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException {
		doGet(request,response);
	}

}
